package com.qfedu.servlet;

import com.qfedu.entity.Address;
import com.qfedu.entity.Result;
import com.qfedu.entity.User;
import com.qfedu.service.IAddressService;
import com.qfedu.service.impl.AddressService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/address")
public class AddressServlet extends BaseServlet {
    private IAddressService addressService = new AddressService();
    public void findAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        Result result = addressService.queryAddressList(uid);
        List<Address> addressList = (List<Address>) result.getDate();
        try {
            if (result.isSuccess()){
                request.getSession().setAttribute("addressList",addressList);
                request.getRequestDispatcher("self_info.jsp").forward(request,response);
            }else {
                request.getSession().setAttribute("addressList",addressList);
                response.sendRedirect("self_info.jsp");
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void addAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        if (name==""||phone==""||detail==""){
            response.sendRedirect("address?method=findAddress&uid= + uid");
            return;
        }
        Result result = addressService.insertAddress(uid, name, phone, detail);
        try {
            if (result.isSuccess()){
                request.getRequestDispatcher("address?method=findAddress&uid=" + uid).forward(request, response);
            }else {
                response.sendRedirect("error.jsp");
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void updateAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        Result result1 = addressService.queryAddress(Integer.parseInt(id));
        Address address = (Address) result1.getDate();
        request.getSession().setAttribute("address",address);
        Result result2 = addressService.updateAddress(Integer.parseInt(id), name, phone, detail);
        try {
            request.getRequestDispatcher("address?method=findAddress&uid=" + uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void deleteAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        String id = request.getParameter("id");
        Result result = addressService.deleteAddress(Integer.parseInt(id));
        try {
            request.getRequestDispatcher("address?method=findAddress&uid=" + uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void setDefault(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        String id = request.getParameter("id");
        Result result = addressService.queryAddressList(uid);
        addressService.queryDefaultAddress(uid,0);
        addressService.setAddressOne(Integer.parseInt(id));
        try {
            request.getRequestDispatcher("address?method=findAddress&uid=" + uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}
